package opl.tables;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class NotificationTable implements ITable {

	public static final String TABLE_NAME = "Notification";
	public static final String FIELD_ID = "Id";
	public static final String FIELD_CAPTION = "Caption";
	public static final String FIELD_DESCRIPTION = "Description";

	@Override
	public void createTable(SQLiteDatabase db) {
		String create_table = "CREATE TABLE " + TABLE_NAME + "(" + FIELD_ID
				+ " INTEGER PRIMARY KEY , " + FIELD_CAPTION
				+ " TEXT NOT NULL, " + FIELD_DESCRIPTION + " TEXT NOT NULL);";

		db.execSQL(create_table);
	}

	@Override
	public void dropTable(SQLiteDatabase db) {
		db.execSQL("DROP TABLE " + TABLE_NAME);
	}

	@Override
	public long insert(ContentValues cv, SQLiteDatabase db) {
		return db.insert(TABLE_NAME, null, cv);
	}

	@Override
	public long update(ContentValues cv, String condition, SQLiteDatabase db) {
		return db.update(TABLE_NAME, cv, condition, null);
	}

	@Override
	public long delete(String condition, SQLiteDatabase db) {
		return db.delete(TABLE_NAME, condition, null);
	}

	@Override
	public Cursor select(String condition, SQLiteDatabase db) {
		return db.query(TABLE_NAME, null, condition, null, null, null, null);
	}
	public Cursor select(String condition,String orderBy,String limit,SQLiteDatabase db){
		//select top 2 * from notifications order by id desc 
		String qry=String.format("select * from %s order by %s desc limit %s", TABLE_NAME,orderBy,limit);
		Log.i("OPL",String.format("Executing query : %s", qry));
		return db.rawQuery(qry,null);
		//return db.query(TABLE_NAME, null, condition, null, null, null, orderBy,limit);
	}
}
